package com.hnxxxy.sysmgr.controller;

import com.github.pagehelper.PageHelper;
import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.common.tools.Help;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.sysmgr.common.Constant;
import com.hnxxxy.sysmgr.pojo.Token;
import com.hnxxxy.sysmgr.service.TokenService;
import com.hnxxxy.sysmgr.vo.TokenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Json
 * @date 2020-26-8:16
 */
@RestController
@RequestMapping("token")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @RequestMapping("loadAllToken")
    @LogAnno(description = "查询所有权限")
    public TableObj queryAllStaffForList(TableObj obj, TokenVo tokenVo){
        PageHelper.startPage(obj.getCurr(), obj.getNums());
        List<Token> list = tokenService.queryAllToken(tokenVo);
        return Help.getPageTableRtn(obj,list);
    }

    /**
     * 添加权限
     */
    @RequestMapping("addToken")
    @LogAnno(description = "添加权限",descriptiontype = "addToken")
    public AjaxRtnEntity addToken(TokenVo tokenVo) {
        tokenVo.setType(Constant.TOKEN_TYPE_TRUE);
        tokenVo.setState(Constant.TOKEN_STATE_TRUE);
        tokenVo.setCreatedate(new Date());
        tokenVo.setCreatedev(new Date());
        return new AjaxRtnEntity(this.tokenService.addToken(tokenVo),"添加成功！",null);

    }
    /**
     * 修改权限
     */
    @RequestMapping("updateToken")
    @LogAnno(description = "更新权限",descriptiontype = "updateToken")
    public AjaxRtnEntity updateToken(TokenVo tokenVo) {
        return new AjaxRtnEntity(this.tokenService.updateToken(tokenVo),"更新成功！",null);

    }

    /**
     * 删除权限
     */
    @RequestMapping("deleteToken")
    @LogAnno(description = "删除权限",descriptiontype = "deleteToken")
    public AjaxRtnEntity deleteToken(Integer id) {

        return new AjaxRtnEntity(this.tokenService.deleteToken(id),"删除成功！",null);

    }

    /**
     * 查询当前的ID的权限有没有子权限
     */
    @RequestMapping("checkTokenHasChildrenNode")
    @LogAnno(description = "判断是否有子权限")
    public Map<String, Object> checkTokenHasChildrenNode(TokenVo tokenVo) {
        Map<String, Object> map = new HashMap<String, Object>();

        Integer count=this.tokenService.queryTokenByPid(tokenVo.getId());
        if (count > 0) {
            map.put("value", true);
        } else {
            map.put("value", false);
        }
        return map;
    }

    /**
     * 批量删除权限
     */
    @RequestMapping("batchDeletetoken")
    @LogAnno(description = "批量删除权限",descriptiontype = "batchDeletetoken")
    public AjaxRtnEntity deleteBatchTokens(TokenVo tokenVo) {
        return new AjaxRtnEntity(this.tokenService.batchDeletetoken(tokenVo.getIds()),"删除成功！",null);
    }
}
